package arsmodeller.editpolicies;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.CreateRequest;

import arsmodeller.commands.AbstractLayoutCommand;
import arsmodeller.commands.BusinessProcessChangeLayoutCommand;
import arsmodeller.commands.BusinessProcessCreateCommand;
import arsmodeller.commands.EmployeeChangeLayoutCommand;
import arsmodeller.commands.EmployeeCreateCommand;
import arsmodeller.commands.ServiceChangeLayoutCommand;
import arsmodeller.commands.ServiceCreateCommand;
import arsmodeller.figure.BusinessProcessFigure;
import arsmodeller.figure.EmployeeFigure;
import arsmodeller.figure.ServiceFigure;
import arsmodeller.model.Connection;
import arsmodeller.part.BusinessProcessPart;
import arsmodeller.part.ConnectionPart;
import arsmodeller.part.EmployeePart;
import arsmodeller.part.EnterprisePart;
import arsmodeller.part.ServicePart;

public class AppEditLayoutPolicy extends XYLayoutEditPolicy {
	@Override
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		AbstractLayoutCommand command = null;
		if (child instanceof EmployeePart) {
			command = new EmployeeChangeLayoutCommand();
		} else if (child instanceof ServicePart) {
			command = new ServiceChangeLayoutCommand();
		} else if (child instanceof BusinessProcessPart) {
			command = new BusinessProcessChangeLayoutCommand();
		}
		command.setModel(child.getModel());
		command.setConstraint((Rectangle) constraint);
		return command;
	}

	@Override
	protected Command getCreateCommand(CreateRequest request) {
		if (request.getType() == REQ_CREATE
				&& getHost() instanceof EnterprisePart) {
			ServiceCreateCommand cmd = new ServiceCreateCommand();
			cmd.setEntreprise(getHost().getModel());
			cmd.setService(request.getNewObject());
			Rectangle constraint = (Rectangle) getConstraintFor(request);
			constraint.x = (constraint.x < 0) ? 0 : constraint.x;
			constraint.y = (constraint.y < 0) ? 0 : constraint.y;
			constraint.width = (constraint.width <= 0) ? ServiceFigure.SERVICE_FIGURE_DEFWIDTH
					: constraint.width;
			constraint.height = (constraint.height <= 0) ? ServiceFigure.SERVICE_FIGURE_DEFHEIGHT
					: constraint.height;
			cmd.setLayout(constraint);
			return cmd;
		}
		 if (request.getType() == REQ_CREATE
				&& getHost() instanceof EmployeePart) {
			EmployeeCreateCommand cmd = new EmployeeCreateCommand();
			cmd.setService(getHost().getModel());
			cmd.setEmploye(request.getNewObject());
			Rectangle constraint = (Rectangle) getConstraintFor(request);
			constraint.x = (constraint.x < 0) ? 0 : constraint.x;
			constraint.y = (constraint.y < 0) ? 0 : constraint.y;
			constraint.width = (constraint.width <= 0) ? EmployeeFigure.EMPLOYEE_FIGURE_DEFWIDTH
					: constraint.width;
			constraint.height = (constraint.height <= 0) ? EmployeeFigure.EMPLOYEE_FIGURE_DEFHEIGHT
					: constraint.height;
			cmd.setLayout(constraint);
			return cmd;
		 }
		 if (request.getType() == REQ_CREATE
				&& getHost() instanceof BusinessProcessPart) {
			BusinessProcessCreateCommand cmd = new BusinessProcessCreateCommand();
			cmd.setEnterprise(getHost().getModel());
			cmd.setBusinessProcess(request.getNewObject());
			Rectangle constraint = (Rectangle) getConstraintFor(request);
			constraint.x = (constraint.x < 0) ? 0 : constraint.x;
			constraint.y = (constraint.y < 0) ? 0 : constraint.y;
			constraint.width = (constraint.width <= 0) ? BusinessProcessFigure.BUSINESSPROCESS_FIGURE_DEFWIDTH
					: constraint.width;
			constraint.height = (constraint.height <= 0) ? BusinessProcessFigure.BUSINESSPROCESS_FIGURE_DEFHEIGHT
					: constraint.height;
			cmd.setLayout(constraint);
			return cmd;
		}
		return null;
	}
}
